#include <stdio.h>
#include <stdlib.h>
typedef struct node{
    int value;
    struct node *next;
}node;

node* newNode(int val) {
    node *Node = (node *)malloc(sizeof(node));
    Node->value = val;
    Node->next = NULL;
    return Node;
}
void printList(node *h){
    node *head=h->next;
    if (!head) {
        printf("\n"); // 空链表直接输出换行
        return;
    }
    while (head) {
        printf("%d", head->value);
        head = head->next;
        if (head) printf(" "); // 只有在后面还有元素时才打印空格
    }
    printf("\n");
    return;
}

node *unionSet(node *h1, node *h2){
    node *l1=h1;
    node *l2=h2;
    while (l1->next!=NULL&&l2->next!=NULL){
        if (l1->next->value<l2->next->value){
            l1=l1->next;
        }
        else if (l1->next->value>l2->next->value){
            node *temp=l2;
            l2=l2->next;
            temp->next=l1->next->next;
            l1->next=temp;
            l1=l1->next;
        }
        else{
            l1=l1->next;
            l2=l2->next;
        }
    }
    if (l2->next!=NULL){
        l1->next=l2->next;
    }
    return h1;
}

node *differenceSet(node *h1,node *h2){
    if (h2->next==NULL)return h1;
    node *l1=h1;
    node *l2=h2;
    while (l1->next!=NULL&&l2->next!=NULL){
        //printf("%d %d\n",l1->value,l2->value);
        if (l1->next->value<l2->next->value){
            l1=l1->next;
        }
        else if (l1->next->value>l2->next->value){
            node *temp=l2;
            l2=l2->next;
            free(temp);
        }
        else{
            node *temp=l1->next;
            l1->next=l1->next->next;
            
            l2=l2->next;
            free(temp);
        }
        //node *ll1=h1;
        //printList(ll1);
    } 
    
    if (l1->next==NULL){
        while (l2->next!=NULL){
            node *temp=l2;
            l2=l2->next;
            free(temp);
        }
        free(l2);
    }
    return h1;
}

node *intersectionSet(node *h1,node *h2){
    if (h1==NULL||h2==NULL)return NULL;
    node *l1=h1,*l2=h2;
    while (l1->next!=NULL&&l2->next!=NULL){
        if (l1->next->value<l2->next->value){
            node *temp=l1->next;
            l1->next=l1->next->next;
            free(temp);
        }
        else if (l1->next->value>l2->next->value){
            node *temp=l2->next;
            l2->next=l2->next->next;
            l2=l2->next;
            free(temp);
        }
        else{
            l1=l1->next;
            l2=l2->next;
        }
    }
    if (l2->next==NULL){
        while (l1->next!=NULL){
            node *temp=l1->next;
            l1->next=l1->next->next;
            free(temp);
        }
    }
    return h1;
}

void insert(node** h, int value) {
    node *temp = newNode(value);
    node **head=&((*h)->next);
    if (*head == NULL || (*head)->value >= value) {
        temp->next = *head;
        *head = temp;
    } else {
        node *current = *head;
        while (current->next != NULL && current->next->value < value)
            current = current->next;
        temp->next = current->next;
        current->next = temp;
    }
}
int main(){
    int n, m, val;
    node *l1 = (node *)malloc(sizeof(node)), *l2 = (node *)malloc(sizeof(node));
    l1->next = NULL;
    l2->next = NULL;
    // 读取第一个集合
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%d", &val);
        insert(&l1, val);
    }

    //node *ll1=l1;
    //printList(ll1);

    // 读取第二个集合
    scanf("%d", &m);
    for (int i = 0; i < m; ++i) {
        scanf("%d", &val);
        insert(&l2, val);
    }

    
    //因为三个函数返回结果存储到l1，对l1本身进行修改，所以不能均运行
    // 计算并打印并集
    /*node* unionResult = unionSet(l1, l2);
    printList(unionResult);*/

    // 计算并打印差集
    node* diffResult = differenceSet(l1, l2);
    printList(diffResult);
    
    // 计算并打印交集
    /*
    node* interResult = intersectionSet(l1, l2);
    printList(interResult);
    return 0;*/
}